#include "UsrAI.h"
#include<set>
#include <iostream>
#include<unordered_map>
using namespace std;
tagGame tagUsrGame;
ins UsrIns;
//
//
//全局变量
tagInfo info;
int centrex=0,centrey=0;    //市镇中心坐标
int homex=0,homey=0;    //房屋坐标
int dockx=0,docky=0;    //船坞坐标
int farmx=0,farmy=0;    //农田坐标
int stockx=0,stocky=0;  //仓库坐标
int landx=0,landy=0;    //己方大陆搜索坐标
int newlandx=0,newlandy=0;  //敌方大陆搜索坐标
int goldx=0,goldy=0;    //金矿坐标
int armycampx=0,armycampy=0;    //兵营坐标
int docknum=0;  //船坞数量
int farmernum=0;    //陆地村民数量
int fishboatnum=0;  //渔船数量
int armyboat=0; //战船数量
int transportboat=0;    //运输船数量
int homenum=0;  //房屋数量
int stock=1;    //仓库数量
int stable=0;   //马厩数量
int armycamp=0; //兵营数量
int range=0;    //靶场数量
int stoneman=0; //投石兵数量
int horse=0;    //骑兵数量
int clubman=0;  //斧头兵数量
int bowman=0;   //弓兵数量
int enemy=0;    //消灭敌兵数量
int upgradearmy=0;  //兵营是否升级
int woodskill=0;    //伐木技术是否升级
int stoneskill=0;   //采石技术是否升级
int goldskill=0;    //掘金技术是否升级
int farmskill=0;    //农田是否升级
int usetool=0;  //使用工具是否升级
int infantrydefense=0;  //步兵护甲是否升级
int cavalrymandefense=0;    //骑兵护甲是否升级
int archerydefense=0;   //弓兵护甲是否升级
int state=0;    //阶段代号
int wood;   //当前木材量
int food;   //当前食物量
int land=0; //初始陆地块数量
int gold=0; //金矿数
int newland=0;  //已探索陆地块数量
int fishfarm=0; //渔场数
int armynum=0;  //地面军队数
char nowmap[128][128];  //当前探索地图
char newmap[128][128];  //新探索地图
int farmerno[20];   //陆地村民SN列表
int boatno[5];  //运输船SN列表
int boatstate[4];   //运输船特殊状态列表
int farmerstate[20];    //陆地村民特殊状态列表
int ele[2]; //大象SN列表
map<int,tagFarmer> farmertable; //储存陆地村民
map<int,tagArmy> armyboatlist;  //储存战船
map<int,tagResource> elephant;  //储存大象
map<int,tagArmy> diearmy;   //储存已消灭敌方陆地军队
map<int,int> worknum;   //储存资源单位工作人数
vector<tagResource> sheep;  //存储活瞪羚
vector<tagResource> sheepst;    //储存瞪羚尸体

int worknumcount(int sn)    //记录单位工作人数
{
    int s=0;
    for(tagFarmer&farmer:info.farmers)
        if(farmer.FarmerSort==0&&farmer.WorkObjectSN==sn)
            s++;
    return s;
}


void getmap()   //获取地图
{
    static int p=0;
    if(!p)
    {
        for(int i=0;i<128;i++)
        {
            for(int j=0;j<128;j++)
            {
                if((*info.theMap)[i][j].type==MAPPATTERN_UNKNOWN)
                    newmap[i][j]='?';
                else newmap[i][j]='*';
            }
        }
        p++;
    }
    //初始化新探索地图
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            if((*info.theMap)[i][j].type==MAPPATTERN_UNKNOWN)
                nowmap[i][j]='?';
            else if((*info.theMap)[i][j].type==MAPPATTERN_OCEAN)
                nowmap[i][j]='-';
            else if((*info.theMap)[i][j].type==MAPPATTERN_GRASS)
            {
                nowmap[i][j]=(*info.theMap)[i][j].height+'0';
                newland++;  //记录已探索陆地块数
            }
        }
    }
    //记录当前探索地图地形信息
    for(tagResource& resource:info.resources)
    {
        if(resource.Type==RESOURCE_FISH||resource.Type==RESOURCE_GOLD||resource.Type==RESOURCE_STONE)
            for(int i=0;i<2;i++)
                for(int j=0;j<2;j++)
                    nowmap[resource.BlockDR+i][resource.BlockUR+j]='r';
        //渔场、石头、金矿2*2
        else
            nowmap[resource.BlockDR][resource.BlockUR]='r';
        worknum[resource.SN]=worknumcount(resource.SN);
        if(resource.Type==RESOURCE_GOLD)
            gold++;
    }
    //记录当前探索地图资源信息
    for(tagFarmer&farmer:info.farmers)
        nowmap[farmer.BlockDR][farmer.BlockUR]='@';
    for(tagArmy&army:info.armies)
        nowmap[army.BlockDR][army.BlockUR]='@';
    //记录当前探索地图陆地单位信息
    for(tagBuilding&building:info.buildings)
    {
        if(building.Type==BUILDING_DOCK||building.Type==BUILDING_HOME||building.Type==BUILDING_ARROWTOWER)
            for(int i=0;i<2;i++)
                for(int j=0;j<2;j++)
                    nowmap[building.BlockDR+i][building.BlockUR+j]=building.Type+'a';
        //2*2建筑
        else
            for(int i=0;i<3;i++)
                for(int j=0;j<3;j++)
                    nowmap[building.BlockDR+i][building.BlockUR+j]=building.Type+'a';;
        //3*3建筑
    }
    //记录当前探索地图建筑信息
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            if(newmap[i][j]!='*')
                newmap[i][j]=nowmap[i][j];
        }
    }
    //若有新探索部分则更新新探索地图
    wood=info.Wood;
    food=info.Meat;
    //初始化资源
    return;
}


int sn_to_obj(int n)    //由SN获取对象类型函数
{
    for(tagResource&resource:info.resources)
        if(resource.SN==n)
            return resource.Type;
    //遍历资源
    for(tagFarmer&farmer:info.farmers)
        if(farmer.SN==n)
            return farmer.FarmerSort;
    //遍历村民
    for(tagArmy&army:info.armies)
        if(army.SN==n)
            return army.Sort;
    //遍历军队
    for(tagBuilding&building:info.buildings)
        if(building.SN==n)
            return building.Type;
    //遍历建筑
}


void gethuman() //获取人员信息
{
    for(tagFarmer&farmer:info.farmers)
    {
        if(farmer.FarmerSort==0)
        {
            if(farmertable.find(farmer.SN)==farmertable.end())
                farmerno[farmernum++]=farmer.SN;
            farmertable[farmer.SN]=farmer;
        }
        if(farmer.FarmerSort==1)
        {
            if(farmertable.find(farmer.SN)==farmertable.end())
                boatno[transportboat++]=farmer.SN;
            farmertable[farmer.SN]=farmer;
        }
    }
    //存储陆地村民与运输船
    for(tagArmy&army:info.armies)
    {
        if(army.Sort==AT_SHIP)
        {
            if(armyboatlist.find(army.SN)==armyboatlist.end())
                armyboat++;
            armyboatlist[army.SN]=army;
        }
        if(army.Sort!=AT_SHIP)
            armynum++;
    }
    //记录地面军队以及战舰数
    for(tagArmy&enemyarmy:info.enemy_armies)
    {
        if(enemyarmy.Sort!=AT_SHIP&&enemyarmy.Blood<=5)
        {
            if(diearmy.find(enemyarmy.SN)==diearmy.end())
                enemy++;
            diearmy[enemyarmy.SN]=enemyarmy;
        }
    }
    //记录已消灭敌方地面军队数
    return;
}


void getresource()  //记录资源信息
{
    for(tagResource&resource:info.resources)
    {
        if(resource.Type==RESOURCE_ELEPHANT&&newmap[resource.BlockDR][resource.BlockUR]=='*')   //判断是否为己方大陆的大象
        {
            if(!ele[0]||resource.SN==ele[0])
            {
                ele[0]=resource.SN;
                elephant[resource.SN]=resource;
                continue;
            }
            if(!ele[1]||resource.SN==ele[1])
            {
                ele[1]=resource.SN;
                elephant[resource.SN]=resource;
                continue;
            }
        }
        //记录大象
        if(resource.Type==RESOURCE_GAZELLE&&newmap[resource.BlockDR][resource.BlockUR]=='*')    //判断是否为己方大陆的瞪羚
        {
            if(resource.Blood>0)
                sheep.push_back(resource);  //活瞪羚
            else if(resource.Cnt>0&&resource.Blood==0)
                sheepst.push_back(resource);    //瞪羚尸体
        }
        //记录瞪羚
    }
}


int surround(int x,int y)   //判断四周情况
{
    int s=0;
    if(nowmap[x][y+1]=='r') //上
        s++;
    if(nowmap[x][y-1]=='r') //下
        s++;
    if(nowmap[x+1][y]=='r') //右
        s++;
    if(nowmap[x-1][y]=='r') //左
        s++;
    return s;
}


bool nearland(int x,int y)   //判断是否临岸（渔场）
{
    if(nowmap[x-1][y]=='0'||nowmap[x-1][y+1]=='0')
        return 1;
    if(nowmap[x+2][y]=='0'||nowmap[x+2][y+1]=='0')
        return 1;
    if(nowmap[x][y-1]=='0'||nowmap[x+1][y-1]=='0')
        return 1;
    if(nowmap[x+1][y+2]=='0'||nowmap[x][y+2]=='0')
        return 1;
    return 0;
}


int findresource(int r,int a,int b,int f=0) //找最近r资源，f代表单位类型，默认为陆地单位
{
    int x=0,y=0,m,n=0;
    for (tagResource&resource:info.resources)
    {
        if (resource.Type==r)
        {
            if(r==RESOURCE_TREE)
            {
                if(surround(resource.BlockDR,resource.BlockUR)>=4)
                    continue;
                if(worknum[resource.SN])
                    continue;
            }
            //找到工作人数等于0，四周未被包围的树
            if(r==RESOURCE_GAZELLE)
            {
                if(resource.Blood>0)
                    continue;
                if(surround(resource.BlockDR,resource.BlockUR)>=4)
                    continue;
                if(worknum[resource.SN]>=3)
                    continue;
            }
            //找到工作人数小于3，四周未被包围的瞪羚尸体
            if(r==RESOURCE_GOLD)
            {
                if(surround(resource.BlockDR,resource.BlockUR)>=4&&surround(resource.BlockDR+1,resource.BlockUR)>=4&&surround(resource.BlockDR,resource.BlockUR+1)>=4&&surround(resource.BlockDR+1,resource.BlockUR+1)>=4)
                    continue;
                if(worknum[resource.SN]>=4-surround(resource.BlockDR,resource.BlockUR))
                    continue;
            }
            //找到工作人数较少，四周未被包围的金矿
            if(r==RESOURCE_ELEPHANT)
            {
                if(resource.Blood>0)
                    continue;
                if(worknum[resource.SN]>=4)
                    continue;
            }
            //找到工作人数小于5，四周未被包围的大象尸体
            if(f==0&&r==RESOURCE_FISH&&!nearland(resource.BlockDR,resource.BlockUR))
                continue;
            //若为陆地单位，找到临岸渔场
            if(n==0)
            {
                n=resource.SN;
                x=resource.BlockDR-a;
                y=resource.BlockUR-b;
                m=x*x+y*y;
            }
            //初始化
            else
            {
                x=resource.BlockDR-a;
                y=resource.BlockUR-b;
                if(m>(x*x+y*y)&&(x*x+y*y)>0)
                {
                    m=x*x+y*y;
                    n=resource.SN;
                }
            }
            //找到更近的
        }
    }
    return n;   //返回找到的资源SN（没有为0）
}


bool ifslope(char n,char a,int ty)  //判断n与a间是否为斜坡，ty为要建造的建筑类型
{
    if(n==a||n=='a'+ty) return 0;   //n,a相同或为同类建筑则不是
    return 1;
}


bool ifspace(int x,int y,int ty)    //判断(x,y)处空地是否能建造ty类型的建筑
{
    char a;
    if(ty==BUILDING_DOCK)   //船坞类型
    {
        a=nowmap[x][y];
        if(a!='-')
            return 0;
        if(nowmap[x+1][y]!=a||nowmap[x][y+1]!=a||nowmap[x+1][y+1]!=a) return 0;
        //是否有2*2海洋
        if(nowmap[x+2][y]>='0'&&nowmap[x+2][y+1]>='0'&&nowmap[x+2][y]<='4'&&nowmap[x+2][y+1]<='4')
            return 1;
        else if(nowmap[x][y+2]>='0'&&nowmap[x+1][y+2]>='0'&&nowmap[x][y+2]<='4'&&nowmap[x+1][y+2]<='4')
            return 1;
        else if(nowmap[x-1][y]>='0'&&nowmap[x-1][y+1]>='0'&&nowmap[x-1][y]<='4'&&nowmap[x-1][y+1]<='4')
            return 1;
        else if(nowmap[x][y-1]>='0'&&nowmap[x+1][y-1]>='0'&&nowmap[x][y-1]<='4'&&nowmap[x+1][y-1]<='4')
            return 1;
        //四周是否有陆地
        return 0;
    }
    else if(ty==BUILDING_ARROWTOWER||ty==BUILDING_HOME) //2*2建筑
    {
        a=nowmap[x][y];
        if(a<'0'||a>'4')
            return 0;
        if(nowmap[x+1][y]!=a||nowmap[x][y+1]!=a||nowmap[x+1][y+1]!=a)
            return 0;
        //是否有2*2陆地
        if(ifslope(nowmap[x+2][y],a,ty)||ifslope(nowmap[x+2][y+1],a,ty))
            return 0;
        else if(ifslope(nowmap[x][y+2],a,ty)||ifslope(nowmap[x+1][y+2],a,ty))
            return 0;
        else if(ifslope(nowmap[x-1][y],a,ty)||ifslope(nowmap[x-1][y+1],a,ty))
            return 0;
        else if(ifslope(nowmap[x][y-1],a,ty)||ifslope(nowmap[x+1][y-1],a,ty))
            return 0;
        else if(ifslope(nowmap[x-1][y-1],a,ty)||ifslope(nowmap[x-1][y+2],a,ty)||ifslope(nowmap[x+2][y+2],a,ty)||ifslope(nowmap[x+2][y-1],a,ty))
            return 0;
        //周围是否有斜坡
        return 1;
    }
    else    //3*3建筑
    {
        a=nowmap[x][y];
        if(a<'0'||a>'4')
            return 0;
        if(nowmap[x][y+1]!=a||nowmap[x][y+2]!=a||nowmap[x+1][y]!=a||nowmap[x+1][y+1]!=a||nowmap[x+1][y+2]!=a||nowmap[x+2][y]!=a||nowmap[x+2][y+1]!=a||nowmap[x+2][y+2]!=a)
            return 0;
        //是否有3*3陆地
        if(ifslope(nowmap[x+3][y],a,ty)||ifslope(nowmap[x+3][y+1],a,ty)||ifslope(nowmap[x+3][y+2],a,ty))
            return 0;
        else if(ifslope(nowmap[x+2][y+3],a,ty)||ifslope(nowmap[x+1][y+3],a,ty)||ifslope(nowmap[x][y+3],a,ty))
            return 0;
        else if(ifslope(nowmap[x-1][y],a,ty)||ifslope(nowmap[x-1][y+1],a,ty)||ifslope(nowmap[x-1][y+2],a,ty))
            return 0;
        else if(ifslope(nowmap[x+2][y-1],a,ty)||ifslope(nowmap[x+1][y-1],a,ty)||ifslope(nowmap[x][y-1],a,ty))
            return 0;
        else if(ifslope(nowmap[x-1][y-1],a,ty)||ifslope(nowmap[x-1][y+3],a,ty)||ifslope(nowmap[x+3][y+3],a,ty)||ifslope(nowmap[x+3][y-1],a,ty))
            return 0;
        //周围是否有斜坡
        return 1;
    }
}


bool findarea(int&p,int&q)  //判断是否在敌方大陆找到建造空地
{
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            int x=i,y=j,ty=BUILDING_ARMYCAMP;
            char a=newmap[x][y];
            if(a<'0'||a>'4')
                continue;
            if(newmap[x][y+1]!=a||newmap[x][y+2]!=a||newmap[x+1][y]!=a||newmap[x+1][y+1]!=a||newmap[x+1][y+2]!=a||newmap[x+2][y]!=a||newmap[x+2][y+1]!=a||newmap[x+2][y+2]!=a)
                continue;
            //是否有3*3空地
            if(ifslope(newmap[x+3][y],a,ty)||ifslope(newmap[x+3][y+1],a,ty)||ifslope(newmap[x+3][y+2],a,ty))
                continue;
            else if(ifslope(newmap[x+2][y+3],a,ty)||ifslope(newmap[x+1][y+3],a,ty)||ifslope(newmap[x][y+3],a,ty))
                continue;
            else if(ifslope(newmap[x-1][y],a,ty)||ifslope(newmap[x-1][y+1],a,ty)||ifslope(newmap[x-1][y+2],a,ty))
                continue;
            else if(ifslope(newmap[x+2][y-1],a,ty)||ifslope(newmap[x+1][y-1],a,ty)||ifslope(newmap[x][y-1],a,ty))
                continue;
            else if(ifslope(newmap[x-1][y-1],a,ty)||ifslope(newmap[x-1][y+3],a,ty)||ifslope(newmap[x+3][y+3],a,ty)||ifslope(newmap[x+3][y-1],a,ty))
                continue;
            //四周是否有斜坡
            p=x;
            q=y;
            return 1;
            //找到则返回1，并通过p,q返回空地坐标
        }
    }
    return 0;   //没找到返回0
}


void findspace(int&x,int&y,int ty)  //找到建造ty建筑的空地，通过x,y返回坐标
{
    int m=128*128+128*128;
    int a,b;
    a=x;
    b=y;
    //遍历地图
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            if(ifspace(i,j,ty)) //判断是否符合要求
            {
                if(m>(i-a)*(i-a)+(j-b)*(j-b)&&(i-a)*(i-a)+(j-b)*(j-b)>=4)
                {
                    x=i;
                    y=j;
                    m=(i-a)*(i-a)+(j-b)*(j-b);
                    //找到离原(x,y)最近的空地坐标
                }
            }
        }
    }
}


bool ifcomplete(int ty) //判断ty建筑是否建完
{
    for(tagBuilding&building:info.buildings)
        if(building.Type==ty&&building.Percent<100)
            return 0;
    //找到建筑且建造进度小于100，返回0
    for(tagBuilding&building:info.buildings)
        if(building.Type==ty)
            return 1;
    //确定建筑存在，返回1
    return 0;
    //建筑不存在返回0
}


bool ifcoast(int x,int y)   //判断是否为沿海陆地
{
    if(nowmap[x][y]!='0') return 0;
    if(nowmap[x+1][y]=='-') return 1;
    if(nowmap[x-1][y]=='-') return 1;
    if(nowmap[x][y+1]=='-') return 1;
    if(nowmap[x][y-1]=='-') return 1;
    return 0;
}


void findcoast(int&x,int&y) //找到沿海陆地，并通过x,y返回
{
    int m=128*128+128*128;
    int a,b;
    a=x;
    b=y;
    //遍历地图
    for(int i=0;i<128;i++)
    {
        for(int j=0;j<128;j++)
        {
            if(ifcoast(i,j))    //判断是否是海岸
            {
                if(m>(i-a)*(i-a)+(j-b)*(j-b)&&(i-a)*(i-a)+(j-b)*(j-b)>5)
                {
                    x=i;
                    y=j;
                    m=(i-a)*(i-a)+(j-b)*(j-b);
                    //找到离原(x,y)最近的海岸
                }
            }
        }
    }
    nowmap[x][y]='+';   //暂时标记
    return;
}


bool findtree() //是否在敌方大陆找到树
{
    //遍历资源
    for(tagResource&resource:info.resources)
        if(newmap[resource.BlockDR][resource.BlockUR]!='*'&&resource.Type==RESOURCE_TREE&&!worknum[resource.SN])
            return 1;
        //判断是否是在敌方大陆树以及是否有人工作
    return 0;
}


bool seaunknown()   //是否有未探索海域
{
    //遍历地图
    for(int j=1;j<127;j++)
        for(int i=1;i<127;i++)
            if(newmap[i][j]=='-'&&(newmap[i+1][j]=='?'||newmap[i][j+1]=='?'||newmap[i-1][j]=='?'||newmap[i][j-1]=='?'))
                return 1;
    return 0;
}


int findenemy(double a,double b)    //找到最近的敌人
{
    int n=0;
    double m;
    for(tagArmy&army:info.enemy_armies)
    {
        if(army.Sort==AT_SHIP&&army.NowState==HUMAN_STATE_ATTACKING&&army.Blood>0)
        {
            if(!n||m>pow((army.DR-a),2)+pow((army.UR-b),2))
            {
                n=army.SN;
                m=pow((army.DR-a),2)+pow((army.UR-b),2);
            }
        }
    }
    //优先找敌方战船

    if(!n)
    {
        for(tagArmy&army:info.enemy_armies)
        {
            if(army.Sort!=AT_SHIP&&army.NowState!=HUMAN_STATE_IDLE&&army.Blood>0)
            {
                if(!n||m>pow((army.DR-a),2)+pow((army.UR-b),2))
                {
                    n=army.SN;
                    m=pow((army.DR-a),2)+pow((army.UR-b),2);
                }
            }
        }
    }
    //其次找敌方地面军队

    if(!n)
    {
        for(tagBuilding&building:info.enemy_buildings)
        {
            if(building.Type==BUILDING_ARROWTOWER&&building.Blood>0)
            {
                if(!n||m>pow((building.BlockDR*BLOCKSIDELENGTH-a),2)+pow((building.BlockUR*BLOCKSIDELENGTH-b),2))
                {
                    n=building.SN;
                    m=pow((building.BlockDR*BLOCKSIDELENGTH-a),2)+pow((building.BlockUR*BLOCKSIDELENGTH-b),2);
                }
            }
        }
    }
    //最后找敌方建筑

    return n;   //返回找到的SN（没有为0）
}


void UsrAI::findland()  //寻找敌方大陆
{
    if(info.armies.size()<=3)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_DOCK&&building.Percent==100)
            {
               if(building.Project==0&&wood>=BUILDING_DOCK_CREATE_SHIP_WOOD&&info.farmers.size()+info.armies.size()<info.Human_MaxNum)
               {
                   BuildingAction(building.SN,BUILDING_DOCK_CREATE_SHIP);
                   wood-=BUILDING_DOCK_CREATE_SHIP_WOOD;
                   break;
               }
            }
        }
    }
    //战船数量过少时补充

    for(tagArmy&army:info.armies)
    {
        if(army.Sort==AT_SHIP&&army.NowState==HUMAN_STATE_IDLE)
        {
            if(newland>land+12)
            {
                newlandx=army.BlockDR;
                newlandy=army.BlockUR;
                goldx=newlandx;
                goldy=newlandy;
                if(wood>=4*BUILD_HOUSE_WOOD)
                {
                    build(farmertable[farmerno[0]],BUILDING_HOME,homex,homey);
                    build(farmertable[farmerno[1]],BUILDING_HOME,homex,homey);
                    build(farmertable[farmerno[2]],BUILDING_HOME,homex,homey);
                    build(farmertable[farmerno[3]],BUILDING_HOME,homex,homey);
                    homenum+=4;
                    state++;
                }
                break;
                //探索到新大陆则标记位置，建造4个房子,进入下一阶段
            }

            else if(centrex>=64&&centrey>=64)
                HumanMove(army.SN,0,0);
            else if(centrex<64&&centrey<64)
                HumanMove(army.SN,127*BLOCKSIDELENGTH,127*BLOCKSIDELENGTH);
            else if(centrex>=64&&centrey<64)
                HumanMove(army.SN,0,127*BLOCKSIDELENGTH);
            else if(centrex<64&&centrey>=64)
                HumanMove(army.SN,127*BLOCKSIDELENGTH,0);
            //根据市镇中心坐标，确定探索方向
        }
    }
    fighting(); //战斗
}


void UsrAI::killsheep(tagFarmer&farmer) //猎杀瞪羚
{
    for(tagResource&sh:sheep)
    {
        if(farmer.WorkObjectSN==sh.SN)
            return;
        //村民工作对象为活瞪羚时返回
    }
    if(sheep.size()>0)
        HumanAction(farmer.SN,sheep.at(0).SN);
    //若存在活瞪羚，则猎杀
}


void UsrAI::search()    //探索海域
{
    static int p=0;
    if(info.armies.size()<=2)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_DOCK&&building.Percent==100)
            {
               if(building.Project==0&&wood>=BUILDING_DOCK_CREATE_SHIP_WOOD&&info.farmers.size()+info.armies.size()<info.Human_MaxNum)
               {
                   BuildingAction(building.SN,BUILDING_DOCK_CREATE_SHIP);
                   wood-=BUILDING_DOCK_CREATE_SHIP_WOOD;
                   break;
               }
            }
        }
    }
    //战船数量过少时补充

    if(seaunknown()&&(enemy<2||enemy>30||armycamp)) //有未探索海域且敌方大陆安全或已消除威胁
    {
        for(tagArmy&army:info.armies)
        {
            if(army.Sort==AT_SHIP&&army.NowState==HUMAN_STATE_IDLE)
            {
                int x=-1,y=-1;
                int m=128*128+128*128;
                for(int j=1;j<127;j++)
                {
                    for(int i=1;i<127;i++)
                    {
                        if(newmap[i][j]=='-'&&(newmap[i+1][j]=='?'||newmap[i][j+1]=='?'||newmap[i-1][j]=='?'||newmap[i][j-1]=='?'))
                        {
                            if(m>(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR))
                            {
                                x=i;
                                y=j;
                                m=(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR);
                            }
                        }
                    }
                }
                //找到战船周围最近的未探索海域
                if(x>=0&&y>=0)
                    HumanMove(army.SN,x*BLOCKSIDELENGTH+10.0,y*BLOCKSIDELENGTH+10.0);
                //若找到则前往
            }
        }
    }
    else if(!seaunknown())  //没有未探索海域
    {
        if(p++<8)   //避免执行次数过多
            for(tagArmy&army:info.armies)
                if(army.Sort==AT_SHIP)
                    HumanMove(army.SN,newlandx*BLOCKSIDELENGTH,newlandy*BLOCKSIDELENGTH);
        //回到敌方大陆标记地
        if(info.armies.size()+info.farmers.size()==info.Human_MaxNum)
        {
            int m=0,sn=0;
            for(tagArmy&army:info.armies)
            {
                if(army.Sort==AT_SHIP)
                {
                    if(sn==0||m>army.Blood)
                    {
                        sn=army.SN;
                        m=army.Blood;
                    }
                }
            }
            HumanAction(sn,sn);
        }
        //若单位数已达上限，销毁血量最低的战舰
    }
    fighting(); //战斗
}


void UsrAI::fishing()   //渔船打鱼
{
    for(tagFarmer&farmer:info.farmers)
    {
        int n=findresource(RESOURCE_FISH,dockx,docky,1);    //找到渔场
        int l1=0,l2=0;
        for(tagResource&resource:info.resources)
        {
            if(resource.SN==n)
                l2=(resource.BlockDR-dockx)*(resource.BlockDR-dockx)+(resource.BlockUR-docky)*(resource.BlockUR-docky);
            if(resource.SN==farmer.WorkObjectSN)
                l1=(resource.BlockDR-dockx)*(resource.BlockDR-dockx)+(resource.BlockUR-docky)*(resource.BlockUR-docky);
            //计算渔船离正在工作与找到的渔场间的距离
        }
        if(farmer.FarmerSort==2&&(l1>l2||farmer.NowState==HUMAN_STATE_IDLE))    //若渔船空闲或找到更近渔场
            HumanAction(farmer.SN,n);
    }
    return;
}


void UsrAI::cuttree(tagFarmer&farmer)   //砍树
{
    int sn=findresource(RESOURCE_TREE,farmer.BlockDR,farmer.BlockUR);   //找到离村民最近的树
    HumanAction(farmer.SN,sn);
    worknum[sn]++;  //该树工作人数+1
    return;
}


void UsrAI::hunting(tagFarmer&farmer,int n) //采集食物
{
    int x,y,sn;
    if(n==RESOURCE_FISH)
        x=dockx,y=docky;
    else x=farmer.BlockDR,y=farmer.BlockUR;
    //根据资源类型确定搜索坐标
    sn=findresource(n,x,y); //找到离搜索坐标最近的资源
    HumanAction(farmer.SN,sn);
    worknum[sn]++;  //资源工作人数+1
    return;
}


int step[2];    //记录打大象的步骤
void UsrAI::killelephant(tagFarmer&farmer,int i)    //猎杀i大象
{
    tagResource e=elephant[ele[i]]; //取大象
    if(step[i]==1&&farmer.NowState==HUMAN_STATE_IDLE)
    {
        HumanAction(farmer.SN,e.SN);
        step[i]=2;
        //完成步骤1，进入步骤2打大象
    }
    if(step[i]==2)
    {
        if((farmer.BlockDR!=e.BlockDR-3&&farmer.BlockDR!=e.BlockDR+3)||(farmer.BlockUR!=e.BlockUR-3&&farmer.BlockUR!=e.BlockUR+3))
            step[i]=0;
        //进入步骤2时，步骤1未完成则回到初始状态
    }
    if(!step[i])
    {
        if(nowmap[e.BlockDR-3][e.BlockUR-3]<='4'&&nowmap[e.BlockDR-3][e.BlockUR-3]>='0')
            HumanMove(farmer.SN,e.DR-2.9*BLOCKSIDELENGTH,e.UR-2.9*BLOCKSIDELENGTH);
        else if(nowmap[e.BlockDR+3][e.BlockUR-3]<='4'&&nowmap[e.BlockDR+3][e.BlockUR-3]>='0')
            HumanMove(farmer.SN,e.DR+2.9*BLOCKSIDELENGTH,e.UR-2.9*BLOCKSIDELENGTH);
        else if(nowmap[e.BlockDR-3][e.BlockUR+3]<='4'&&nowmap[e.BlockDR-3][e.BlockUR+3]>='0')
            HumanMove(farmer.SN,e.DR-2.9*BLOCKSIDELENGTH,e.UR+2.9*BLOCKSIDELENGTH);
        else if(nowmap[e.BlockDR+3][e.BlockUR+3]<='4'&&nowmap[e.BlockDR+3][e.BlockUR+3]>='0')
            HumanMove(farmer.SN,e.DR+2.9*BLOCKSIDELENGTH,e.UR+2.9*BLOCKSIDELENGTH);
        step[i]++;
        //初始状态时，进入步骤1，移动到大象视野盲区
    }
}


void UsrAI::build(tagFarmer&farmer,int ty,int&x,int&y)  //在(x,y)附近建造ty建筑
{
    findspace(x,y,ty);  //找到空地
    HumanBuild(farmer.SN,ty,x,y);
    if(ty==BUILDING_ARROWTOWER||ty==BUILDING_HOME)
        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                nowmap[x+i][y+j]='B';
    //在地图上标记2*2建筑
    else
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
                nowmap[x+i][y+j]='B';
    //在地图上标记2*2建筑
    return;
}


void UsrAI::landing()   //运输船登陆
{
    for(int i=0;i<transportboat;i++)
    {

        if(((boatstate[i]==0&&farmertable[boatno[i]].NowState==HUMAN_STATE_IDLE)||boatstate[i]==1)&&farmertable[boatno[i]].Resource>0)  //运输船有人且初次运输或上次运输失败
        {
            findcoast(newlandx,newlandy);   //找到登陆海岸
            if((*info.theMap)[newlandx-1][newlandy].type==MAPPATTERN_GRASS&&(*info.theMap)[newlandx][newlandy-1].type==MAPPATTERN_GRASS)
            {
                newlandx++;
                newlandy++;
                HumanMove(boatno[i],newlandx*BLOCKSIDELENGTH-0.5,newlandy*BLOCKSIDELENGTH-0.5);
                boatstate[i]=0;
            }
            else if((*info.theMap)[newlandx+1][newlandy].type==MAPPATTERN_GRASS&&(*info.theMap)[newlandx][newlandy+1].type==MAPPATTERN_GRASS)
            {
                HumanMove(boatno[i],newlandx*BLOCKSIDELENGTH+0.5,newlandy*BLOCKSIDELENGTH+0.5);
                boatstate[i]=0;
            }
            else if((*info.theMap)[newlandx+1][newlandy].type==MAPPATTERN_GRASS&&(*info.theMap)[newlandx][newlandy-1].type==MAPPATTERN_GRASS)
            {
                newlandy++;
                HumanMove(boatno[i],newlandx*BLOCKSIDELENGTH+0.5,newlandy*BLOCKSIDELENGTH-0.5);
                boatstate[i]=0;
            }
            else if((*info.theMap)[newlandx-1][newlandy].type==MAPPATTERN_GRASS&&(*info.theMap)[newlandx][newlandy+1].type==MAPPATTERN_GRASS)
            {
                newlandx++;
                HumanMove(boatno[i],newlandx*BLOCKSIDELENGTH-0.5,newlandy*BLOCKSIDELENGTH+0.5);
                boatstate[i]=0;
            }
            //根据海岸周围情况，确定登录细节坐标
        }
    }
}


void UsrAI::searcharea()    //探索新大陆
{
    int x,y;
    static int a=newlandx,b=newlandy;
    int m=128*128+128*128;
    for(int j=1;j<127;j++)
    {
        for(int i=1;i<127;i++)
        {
            if((newmap[i][j]>='0'&&newmap[i][j]<='4')&&(nowmap[i+1][j]=='?'||nowmap[i][j+1]=='?'||nowmap[i-1][j]=='?'||nowmap[i][j-1]=='?'))    //未探索陆地
            {
                if(m>(i-a)*(i-a)+(j-b)*(j-b)&&(i-a)*(i-a)+(j-b)*(j-b)>2)
                {
                    x=i;
                    y=j;
                    m=(i-a)*(i-a)+(j-b)*(j-b);
                }
            }
        }
    }
    //找到离当前位置最近的未探索陆地

    for(int i=0;i<2;i++)
    {
        if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
        {
            HumanMove(farmerno[i],x*BLOCKSIDELENGTH,y*BLOCKSIDELENGTH);
            a=x;
            b=y;
        }
    }
    //移动到未探索陆地，并更新当前位置

    for(tagBuilding&building:info.buildings)
    {
        if((building.Type==BUILDING_ARMYCAMP||building.Type==BUILDING_RANGE||building.Type==BUILDING_STOCK||building.Type==BUILDING_STABLE)&&building.Percent<100)
        {
            for(int i=0;i<2;i++)
            {
                if(farmertable[farmerno[i]].WorkObjectSN!=building.SN)
                    HumanAction(farmerno[i],building.SN);
            }
            return;
        }
    }
    //若有未建完建筑，则登陆2人去建

    int p=0,q=0;
    if(armycamp==0||range==0||stock<3||stable==0)
    {
        if(enemy>25) range=1,stable=1; //若敌方大陆地面威胁已很小，则只建兵营与仓库
        if(findarea(p,q))   //找到敌方大陆空地
        {
            //建兵营
            if(armycamp==0&&wood>=BUILD_ARMYCAMP_WOOD)
            {
                for(int i=0;i<2;i++)
                {
                    if(farmertable[farmerno[i]].NowState==HUMAN_STATE_WALKING)
                    {
                        HumanBuild(farmerno[i],BUILDING_ARMYCAMP,p,q);
                        wood-=BUILD_ARMYCAMP_WOOD;
                        armycamp++;
                        armycampx=p;
                        armycampy=q;
                        return;
                    }
                }
            }

            //兵营建完建靶场
            if(!range&&armycamp&&ifcomplete(BUILDING_ARMYCAMP)&&wood>=BUILD_RANGE_WOOD)
            {
                for(int i=0;i<2;i++)
                {
                    if(farmertable[farmerno[i]].NowState==HUMAN_STATE_WALKING)
                    {
                        HumanBuild(farmerno[i],BUILDING_RANGE,p,q);
                        wood-=BUILD_RANGE_WOOD;
                        range++;
                        return;
                    }
                }
            }

            //建马厩
            if(armycamp&&!stable&&range&&wood>=BUILD_STABLE_WOOD)
            {
                for(int i=0;i<2;i++)
                {
                    if(farmertable[farmerno[i]].NowState==HUMAN_STATE_WALKING)
                    {
                        HumanBuild(farmerno[i],BUILDING_STABLE,p,q);
                        wood-=BUILD_STABLE_WOOD;
                        stable++;
                        return;
                    }
                }
            }

            //建仓库
            if(stock<3&&armycamp&&range&&wood>BUILD_STOCK_WOOD)
            {
                for(int i=0;i<2;i++)
                {
                    if(farmertable[farmerno[0]].NowState==HUMAN_STATE_WALKING)
                    {
                        HumanBuild(farmerno[0],BUILDING_STOCK,p,q);
                        stock++;
                        wood-=BUILD_STOCK_WOOD;
                        return;
                    }
                }
            }
        }
    }
    else
    {
        if(gold)
        {
            for(int i=0;i<2;i++)
            {
                if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE&&sn_to_obj(farmertable[farmerno[i]].WorkObjectSN))
                {
                    mininggold(farmertable[farmerno[i]]);
                    return;
                    //若找到金矿则挖金矿
                }
            }
        }
        else if(gold==0&&findtree())
        {
            for(int i=0;i<2;i++)
            {
                if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
                {
                    cuttree(farmertable[farmerno[i]]);
                    return;
                    //若没有金矿有树则砍树
                }
            }
        }
    }
}


void UsrAI::makearmy()  //生产军队
{
    //兵营未升级则升级兵营
    if(!upgradearmy)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_ARMYCAMP&&building.Percent==100)
            {
                if(building.Project==0)
                {
                    if(food>=BUILDING_ARMYCAMP_UPGRADE_CLUBMAN_FOOD)
                    {
                        BuildingAction(building.SN,BUILDING_ARMYCAMP_UPGRADE_CLUBMAN);
                        upgradearmy=building.SN;
                        food-=BUILDING_ARMYCAMP_UPGRADE_CLUBMAN_FOOD;
                        break;
                    }
                }
            }
        }
    }
    for(tagBuilding&building:info.buildings)
    {
        if((usetool&&archerydefense&&infantrydefense&&cavalrymandefense)||enemy>25) //护甲技术已升级或敌方大陆地面威胁很小时造兵
        {
            if(building.Type==BUILDING_ARMYCAMP&&building.Percent==100)
            {
                if(building.Project==0)
                {
                    if(food>=BUILDING_ARMYCAMP_CREATE_SLINGER_FOOD&&stoneman<5) //食物充足且未达既定数量
                    {
                        BuildingAction(building.SN,BUILDING_ARMYCAMP_CREATE_SLINGER);
                        food-=BUILDING_ARMYCAMP_CREATE_SLINGER_FOOD;
                        stoneman++;
                        continue;
                        //造投石兵
                    }
                    else if(food>=BUILDING_ARMYCAMP_CREATE_CLUBMAN_FOOD&&((clubman<10&&armynum<25)||info.armies.size()<12)) //食物充足且未达既定数量或兵力过少
                    {
                        BuildingAction(building.SN,BUILDING_ARMYCAMP_CREATE_CLUBMAN);
                        food-=BUILDING_ARMYCAMP_CREATE_CLUBMAN_FOOD;
                        clubman++;
                        continue;
                        //造近战兵
                    }
                }
            }
            if(building.Type==BUILDING_RANGE&&building.Percent==100)
            {
                if(building.Project==0)
                {
                    if(food>=BUILDING_RANGE_CREATE_BOWMAN_FOOD&&wood>=BUILDING_RANGE_CREATE_BOWMAN_WOOD&&((bowman<10&&armynum<25)||info.armies.size()<12))  //食物充足且未达既定数量或兵力过少
                    {
                        BuildingAction(building.SN,BUILDING_RANGE_CREATE_BOWMAN);
                        wood-=BUILDING_RANGE_CREATE_BOWMAN_WOOD;
                        food-=BUILDING_RANGE_CREATE_BOWMAN_FOOD;
                        bowman++;
                        continue;
                        //造弓兵
                    }
                }
            }
            if(building.Type==BUILDING_STABLE&&building.Percent==100)
            {
                if(building.Project==0)
                {
                    if(food>=BUILDING_STABLE_CREATE_SCOUT_FOOD&&horse<2)    //食物充足且未达既定数量
                    {
                        BuildingAction(building.SN,BUILDING_STABLE_CREATE_SCOUT);
                        food-=BUILDING_STABLE_CREATE_SCOUT_FOOD;
                        horse++;
                        continue;
                        //造骑兵
                    }
                }
            }
        }
        if(building.Type==BUILDING_STOCK&&building.Project==0&&enemy<25)    //敌方大陆威胁较大时
        {
            if(food>=BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY_FOOD&&!infantrydefense)
            {
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY);
                food-=BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY_FOOD;
                infantrydefense++;
                continue;
                //升级步兵护甲
            }
            if(food>=BUILDING_STOCK_UPGRADE_USETOOL_FOOD&&!usetool)
            {
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_USETOOL);
                food-=BUILDING_STOCK_UPGRADE_USETOOL_FOOD;
                usetool++;
                continue;
                //升级使用工具
            }
            if(food>=BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER_FOOD&&!archerydefense)
            {
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER);
                food-=BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER_FOOD;
                archerydefense++;
                continue;
                //升级弓兵护甲
            }
            if(food>=BUILDING_STOCK_UPGRADE_DEFENSE_RIDER_FOOD&&!cavalrymandefense)
            {
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_RIDER);
                food-=BUILDING_STOCK_UPGRADE_DEFENSE_RIDER_FOOD;
                cavalrymandefense++;
                continue;
                //升级骑兵护甲
            }
       }
       if(building.Type==BUILDING_MARKET&&building.Project==0)
       {
            if(food>=BUILDING_MARKET_STONE_UPGRADE_FOOD&&!stoneskill)
            {
                BuildingAction(building.SN,BUILDING_MARKET_STONE_UPGRADE);
                food-=BUILDING_MARKET_STONE_UPGRADE_FOOD;
                stoneskill++;
                continue;
                //升级采石技术
            }
           if(food>=BUILDING_MARKET_GOLD_UPGRADE_FOOD&&!goldskill)
           {
               BuildingAction(building.SN,BUILDING_MARKET_GOLD_UPGRADE);
               food-=BUILDING_MARKET_GOLD_UPGRADE_FOOD;
               goldskill++;
               continue;
               //升级掘金技术
           }
       }
    }
}


void UsrAI::findgold()  //找金矿
{
    for(tagArmy&army:info.armies)
    {
        if(army.Sort!=AT_SHIP&&army.NowState==HUMAN_STATE_IDLE)
        {
            int x=-1,y=-1;
            int m=128*128+128*128;
            for(int j=1;j<127;j++)
            {
                for(int i=1;i<127;i++)
                {
                    if((newmap[i][j]>='0'&&newmap[i][j]<='4')&&(newmap[i+1][j]=='?'||newmap[i][j+1]=='?'||newmap[i-1][j]=='?'||newmap[i][j-1]=='?'))    //未探索大陆
                    {
                        if(m>(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR))
                        {
                            x=i;
                            y=j;
                            m=(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR);
                        }
                    }
                }
            }
            if(x>=0&&y>=0)
                HumanMove(army.SN,x*BLOCKSIDELENGTH+10.0,y*BLOCKSIDELENGTH+10.0);
            //移动到最近的未探索大陆
        }

        if(gold>6&&enemy>=25)   //找到矿脉且敌方大陆威胁消除
        {
            state++;
            goldx=army.BlockDR;
            goldy=army.BlockUR;
            stockx=goldx;
            stocky=goldy;
            break;
            //找到金矿后标记金矿位置，进入下一阶段
        }
    }
    fighting(); //战斗
}


void UsrAI::mininggold(tagFarmer&farmer)    //挖金矿
{
    int sn=findresource(RESOURCE_GOLD,goldx,goldy); //找到最近金矿
    HumanAction(farmer.SN,sn);
    worknum[sn]++;  //工作人数+1
    return;
}


void UsrAI::getgold()   //获取黄金直到胜利
{
    for(tagBuilding&building:info.buildings)
    {
        if(building.Percent<100&&building.Type==BUILDING_STOCK)
        {
            for(int i=2;i<6;i++)
            {
                if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=BUILDING_STOCK)
                    HumanAction(farmertable[farmerno[i]].SN,building.SN);
            }
            break;
        }
    }
    //若有未完成的建筑，派人建造

    if(info.farmers.size()>16)
    {
        if(stock>=5)
        {
            for(tagFarmer&farmer:info.farmers)
            {
                if(farmer.FarmerSort==0&&farmer.NowState==HUMAN_STATE_IDLE||sn_to_obj(farmer.WorkObjectSN)==RESOURCE_TREE)
                    mininggold(farmer);
                //仓库建好后所有人挖金矿
            }
        }

        if(stock<5&&wood>=340)
        {
            build(farmertable[farmerno[3]],BUILDING_STOCK,goldx,goldy);
            build(farmertable[farmerno[2]],BUILDING_STOCK,goldx,goldy);
            wood-=2*BUILD_STOCK_WOOD;
            stock+=2;
            //木材充足时建仓库
        }
        else if(stock<5&&wood<340)
        {
            for(tagFarmer&farmer:info.farmers)
            {
                if(farmer.FarmerSort==0&&(sn_to_obj(farmer.WorkObjectSN)!=RESOURCE_TREE||farmer.NowState==HUMAN_STATE_IDLE))
                {
                    cuttree(farmer);
                }
            }
            //木材不足且仓库不够时砍树
        }
    }
    return;
}


void UsrAI::fighting()  //战斗
{
    for(tagArmy&army:info.armies)
    {
        if(findenemy(army.BlockDR,army.BlockUR)&&army.WorkObjectSN!=findenemy(army.DR,army.UR))
            HumanAction(army.SN,findenemy(army.DR,army.UR));
        //找到最近的敌人攻击
        if(!findenemy(army.DR,army.UR)&&army.NowState==HUMAN_STATE_ATTACKING)
            HumanMove(army.SN,army.DR,army.UR);
        //没有敌人或敌军逃跑则退出攻击
    }
}


void UsrAI::f0()    //游戏开始
{
    for(int i=0;i<128;i++)
        for(int j=0;j<128;j++)
            if((*info.theMap)[i][j].type==MAPPATTERN_GRASS)
                land++; //记录己方大陆陆地块数量
    if (centrex==0&&centrey==0)
    {
        for (tagBuilding&building:info.buildings)
        {
            if (building.Type==BUILDING_CENTER)
            {
                centrex=building.BlockDR+1;
                centrey=building.BlockUR+1;
            }
            if(building.Type==BUILDING_STOCK)
            {
                stockx=building.BlockDR;
                stocky=building.BlockUR;
            }
        }
        homex=centrex;
        homey=centrey;
        dockx=centrex;
        docky=centrey;
        farmx=stockx;
        farmy=stocky;
        landx=centrex;
        landy=centrey;
        //记录市镇中心、仓库等坐标
    }
    build(farmertable[farmerno[0]],BUILDING_DOCK,dockx,docky);  //造船坞
    docknum++;
    build(farmertable[farmerno[1]],BUILDING_HOME,homex,homey);  //造房屋
    homenum++;
    killsheep(farmertable[farmerno[2]]);    //杀瞪羚
    farmerstate[2]=1;
    cuttree(farmertable[farmerno[3]]);  //砍树
    state++;    //进入下一阶段
    return;
}


void UsrAI::f1()    //阶段1
{
    static int upgrade=0;   //记录时代升级状态

    if(!upgrade)    //未开始升级时代
    {
        for (tagBuilding&building:info.buildings)
        {
            if (building.Type==BUILDING_CENTER&&building.Project==0&&food>=500)
            {
                BuildingAction(building.SN,BUILDING_CENTER_UPGRADE);
                upgrade++;
                food-=500;
                break;
                //食物充足时升级时代
            }
        }
        for(tagBuilding&building:info.buildings)
        {
            if(building.Percent<100&&building.Type==BUILDING_HOME&&farmertable[farmerno[1]].WorkObjectSN!=building.SN)
            {
                HumanAction(farmertable[farmerno[1]].SN,building.SN);
                break;
                //有未建完建筑，则派人去建
            }
        }
    }

    if(upgrade==2)  //正在升级时代
    {
        for(int i=0;i<farmernum;i++)
            if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
                cuttree(farmertable[farmerno[i]]);
        //所有空闲村民去砍树
        if(sheepst.size()>0)
        {
            for(int i=10;i<12;i++)
                if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=RESOURCE_GAZELLE)
                    hunting(farmertable[farmerno[i]],RESOURCE_GAZELLE);
            //瞪羚尸体未采集完则留2人采集
        }
        for(tagBuilding&building:info.buildings)
        {
            if(building.Percent<100)
            {
                for(int i=0;i<=3;i++)
                {
                    if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)==RESOURCE_TREE)
                        HumanAction(farmertable[farmerno[i]].SN,building.SN);
                }
                break;
                //有未建完建筑，则派人去建
            }
        }
        if(info.civilizationStage==CIVILIZATION_TOOLAGE)
        {
            if(wood>BUILD_GRANARY_WOOD)
            {
                build(farmertable[farmerno[0]],BUILDING_GRANARY,stockx,stocky);
                state++;
                //进入工具时代且建造了谷仓则进入下一阶段
            }
        }
        return;
    }

    if(upgrade==1)  //开始升级时代
    {
        for(tagFarmer&farmer:info.farmers)
            if(farmer.FarmerSort==0&&(sn_to_obj(farmer.WorkObjectSN)!=RESOURCE_TREE&&sn_to_obj(farmer.WorkObjectSN)!=RESOURCE_GAZELLE))
                cuttree(farmer);
        //所有人除了采集瞪羚的取砍树
        if(wood>=3*BUILD_HOUSE_WOOD)
        {
            build(farmertable[farmerno[1]],BUILDING_HOME,homex,homey);
            build(farmertable[farmerno[2]],BUILDING_HOME,homex,homey);
            build(farmertable[farmerno[3]],BUILDING_HOME,homex,homey);
            homenum+=3;
            upgrade++;
            //木材足够时造三个房屋
        }
        return;
    }
    if(farmertable[farmerno[0]].NowState==HUMAN_STATE_IDLE)
       hunting(farmertable[farmerno[0]],RESOURCE_GAZELLE);  //建船坞的村民建完后去采集瞪羚
    if(farmertable[farmerno[1]].NowState==HUMAN_STATE_IDLE&&homenum>=3&&stock>=2)
        hunting(farmertable[farmerno[1]],RESOURCE_GAZELLE); //建房子的村民建完房屋与仓库后去采集瞪羚
    else if(farmertable[farmerno[1]].NowState==HUMAN_STATE_IDLE&&homenum>=3&&stock<2)
        cuttree(farmertable[farmerno[1]]);  //建房子的村民建完房屋还未建仓库时去砍树
    if(homenum<3)
    {
        if(farmertable[farmerno[1]].NowState==HUMAN_STATE_IDLE&&wood>=BUILD_HOUSE_WOOD&&info.Human_MaxNum==homenum*4+4)
        {
            build(farmertable[farmerno[1]],BUILDING_HOME,homex,homey);
            homenum++;
            wood-=BUILD_HOUSE_WOOD;
            //房屋未满3个时继续造
        }
    }
    if(stock<2&&homenum>=3)
    {
        if(sn_to_obj(farmertable[farmerno[1]].WorkObjectSN)!=BUILDING_HOME&&wood>=BUILD_STOCK_WOOD)
        {
            for(tagResource&resource:info.resources)
            {
                if(resource.Type==RESOURCE_GAZELLE)
                {
                    build(farmertable[farmerno[1]],BUILDING_STOCK,resource.BlockDR,resource.BlockUR);
                    wood-=BUILD_STOCK_WOOD;
                    stock++;
                    break;
                    //房屋建完、木材足够时，建房子的村民去建仓库
                }
            }
        }
    }
    if(sheep.size()>0)
        killsheep(farmertable[farmerno[2]]);
    else
        farmerstate[2]=0;
    //瞪羚未杀完则继续杀，否则村民状态恢复正常
    if(farmernum<12)
    {
        for (tagBuilding&building:info.buildings)
        {
            if (building.Type==BUILDING_CENTER&&building.Project==0&&food>=BUILDING_CENTER_CREATEFARMER_FOOD&&info.farmers.size()<info.Human_MaxNum)
            {
                BuildingAction(building.SN,BUILDING_CENTER_CREATEFARMER);
                food-=BUILDING_CENTER_CREATEFARMER_FOOD;
                break;
                //陆地村民少于12个且食物充足人数未达上限时生产村民
            }
        }
    }

    if(fishboatnum<2&&homenum>=3)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_DOCK&&building.Percent==100)
            {
               if(building.Project==0&&wood>=BUILDING_DOCK_CREATE_SAILING_WOOD&&info.farmers.size()<info.Human_MaxNum)
               {
                   BuildingAction(building.SN,BUILDING_DOCK_CREATE_SAILING);
                   fishboatnum++;
                   wood-=BUILDING_DOCK_CREATE_SAILING_WOOD;
                   break;
                   //已有船坞时，渔船少于2艘且木材足够单位数未达上限则造渔船
               }
            }
        }
    }

    for (int i=3;i<farmernum;i++)
    {
       if(i%2&&sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=RESOURCE_GAZELLE&&stock>=2)
           hunting(farmertable[farmerno[i]],RESOURCE_GAZELLE);
       else if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
           cuttree(farmertable[farmerno[i]]);
       //仓库造好后再分配5人采集瞪羚，4人砍树
    }
    fishing();  //渔船打鱼
    return;
}


void UsrAI::f2()    //阶段2
{
    static int market=0;    //记录市场状态
    for(int i=0;i<farmernum;i++)
    {
        if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
            cuttree(farmertable[farmerno[i]]);
        //有空闲村民则去砍树
    }
    if(sheepst.size()>0)
    {
        for(int i=10;i<12;i++)
            if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=RESOURCE_GAZELLE)
                hunting(farmertable[farmerno[i]],RESOURCE_GAZELLE);
            //有瞪羚尸体，则分配2人采集
    }
    if(food<100&&!woodskill&&market&&docknum==2)
    {
        if(farmertable[farmerno[0]].NowState==HUMAN_STATE_IDLE||sn_to_obj(farmertable[farmerno[0]].WorkObjectSN)==RESOURCE_TREE)
            hunting(farmertable[farmerno[0]],RESOURCE_GAZELLE);
        //食物紧缺时再分配1人去采集瞪羚
    }
    else if(woodskill)
        if(sn_to_obj(farmertable[farmerno[0]].WorkObjectSN)!=RESOURCE_TREE&&sn_to_obj(farmertable[farmerno[0]].WorkObjectSN)!=BUILDING_DOCK)
            cuttree(farmertable[farmerno[0]]);
    //伐木技术升级后，额外分配采集瞪羚的村民去砍树
    for(tagBuilding&building:info.buildings)
    {
        if(building.Percent<100)
        {
            for(int i=0;i<=3;i++)
            {
                if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)==RESOURCE_TREE)
                    HumanAction(farmertable[farmerno[i]].SN,building.SN);
            }
            break;
            //有未建完建筑则分配人去建
        }
    }
    if(!market) //没有市场
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_GRANARY)
            {
                if(building.Percent==100&&wood>=BUILD_MARKET_WOOD)
                {
                    build(farmertable[farmerno[0]],BUILDING_MARKET,stockx,stocky);
                    wood-=BUILD_MARKET_WOOD;
                    market=1;
                    //谷仓建完建市场
                }
            }
        }
    }
    for(tagBuilding&building:info.buildings)
    {
        if(building.Type==BUILDING_MARKET&&building.Percent==100)
        {
            market=2;
            if(building.Project==0)
            {
                if(food>=120&&wood>=75&&woodskill==0)
                {
                    BuildingAction(building.SN,BUILDING_MARKET_WOOD_UPGRADE);
                    woodskill++;
                    food-=120;
                    wood-=75;
                    //市场建完且资源充足则升级伐木技术
                }
            }
        }
    }
    if(market&&docknum<2)
    {
        if(wood>=BUILD_DOCK_WOOD&&sn_to_obj(farmertable[farmerno[1]].NowState)!=BUILDING_DOCK)
        {
            build(farmertable[farmerno[1]],BUILDING_DOCK,dockx,docky);
            docknum++;
            wood-=BUILD_DOCK_WOOD;
            //市场建完再建一个船坞
        }
    }
    if(armyboat<10&&armyboat+info.farmers.size()<info.Human_MaxNum&&market)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_DOCK&&building.Percent==100)
            {
               if(building.Project==0&&wood>=BUILDING_DOCK_CREATE_SHIP_WOOD&&info.farmers.size()+info.armies.size()<info.Human_MaxNum)
               {
                   BuildingAction(building.SN,BUILDING_DOCK_CREATE_SHIP);
                   wood-=BUILDING_DOCK_CREATE_SHIP_WOOD;
                   continue;
                   //战船数少于10艘且单位数未达上限、资源充足则造战船
               }
            }
        }
    }
    else if(armyboat+info.farmers.size()>=28||armyboat>=10)
        findland(); //战船数量达到设计上限或人口上限则出海探索
    static int br=0;
    if(armyboat>=7&&!br)
    {
        for(tagArmy&army:info.armies)
        {
            if(army.Sort==AT_SHIP&&army.NowState==HUMAN_STATE_IDLE)
                HumanMove(army.SN,centrex*BLOCKSIDELENGTH,centrey*BLOCKSIDELENGTH);
        }
        br++;
        //战船数量达到7时移动一下，防止堵塞船坞
    }
    if(farmernum<16&&woodskill)
    {
        for (tagBuilding&building:info.buildings)
        {
            if (building.Type==BUILDING_CENTER&&building.Project==0&&food>=BUILDING_CENTER_CREATEFARMER_FOOD&&info.farmers.size()<info.Human_MaxNum)
            {
                BuildingAction(building.SN,BUILDING_CENTER_CREATEFARMER);
                food-=BUILDING_CENTER_CREATEFARMER_FOOD;
                break;
                //村民数量少于16且资源充足、人口未达上限则生产村民
            }
        }
    }
    fishing();  //渔船打鱼
    return;
}


void UsrAI::f3()    //阶段3
{   
    static int farm=0;  //记录农田数量
    static int outsea=0;    //记录是否出发登陆
    static int out=0;   //记录是否出发找金矿
    search();   //探索海域
    fishing();  //渔船打鱼
    for(int i=2;i<farmernum;i++)
        if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
            cuttree(farmertable[farmerno[i]]);
    //有人空闲则去砍树
    for(tagBuilding&building:info.buildings)
    {
        if(building.Percent<100&&building.Type==BUILDING_HOME)
        {
            for(int i=2;i<=3;i++)
            {
                if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)==RESOURCE_TREE)
                    HumanAction(farmertable[farmerno[i]].SN,building.SN);
            }
            break;
            //有未建完建筑则派人去建
        }
    }
    if(transportboat<1)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_DOCK&&building.Project==0)
            {
                if(wood>=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD)
                {
                    BuildingAction(building.SN,BUILDING_DOCK_CREATE_WOOD_BOAT);
                    wood-=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD;
                    break;
                    //运输船数量少于1，木材足够则造运输船
                }
            }
        }
    }
    if(!outsea&&transportboat>=1)
    {
        if(farmertable[boatno[0]].Resource==0&&farmertable[boatno[0]].NowState==HUMAN_STATE_IDLE&&!boatstate[0])
        {
            findcoast(landx,landy);
            HumanMove(boatno[0],BLOCKSIDELENGTH*landx+10.0,BLOCKSIDELENGTH*landy+10.0);
            HumanAction(farmerno[0],boatno[0]);
            HumanAction(farmerno[1],boatno[0]);
            boatstate[0]=1;
            //运输船靠岸，2个村民上船
        }
        if(farmertable[boatno[0]].Resource==2&&boatstate[0]==1)
        {
            outsea++;
            landx=centrex;
            landy=centrey;
            //村民完成上船则出发登陆
        }
    }
    if(outsea)
    {
        landing();  //登陆
        if(farmertable[boatno[0]].Resource==0&&farmertable[boatno[0]].NowState==HUMAN_STATE_IDLE&&!boatstate[0])
        {
            findcoast(landx,landy);
            HumanMove(boatno[0],BLOCKSIDELENGTH*landx+15.0,BLOCKSIDELENGTH*landy+15.0);
            boatstate[0]=1;
            //登陆完成后运输船回来
        }
        searcharea();   //登陆村民开始探索敌方大陆
    }
    if(homenum<11&&wood>=BUILD_HOUSE_WOOD)
    {
        build(farmertable[farmerno[2]],BUILDING_HOME,homex,homey);
        wood-=BUILD_HOUSE_WOOD;
        homenum++;
        //房屋数少于11且木材充足则造房屋
    }
    for(int i=4;i<9;i++)
    {
        if(farm<5&&wood>=BUILD_FARM_WOOD&&sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=BUILDING_FARM&&((armycamp&&range&&stock>=3&&stable)||wood>=550))
        {
            build(farmertable[farmerno[i]],BUILDING_FARM,farmx,farmy);
            farm++;
            wood-=BUILD_FARM_WOOD;
            continue;
            //农田数量少于5且木材充足则分配5人开垦农田
        }
        else if(farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
            cuttree(farmertable[farmerno[i]]);  //否则去砍树
    }
    if(!out)
    {
        static int q=0;
        for(int i=10;i<farmernum;i++)
        {
            if(((armycamp&&range&&stock>=3&&stable)||wood>400)&&((elephant[ele[0]].Cnt>0&&elephant[ele[0]].Blood==0)||(elephant[ele[1]].Cnt>0&&elephant[ele[1]].Blood==0)))
            {
                if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=RESOURCE_ELEPHANT)
                    hunting(farmertable[farmerno[i]],RESOURCE_ELEPHANT);
                //有大象尸体且木材不紧缺时分配6人采集大象
            }
            else if(sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=RESOURCE_TREE)
                cuttree(farmertable[farmerno[i]]);  //否则分配6人砍树
        }
        if(elephant[ele[0]].Blood==0)
        {
            if(elephant[ele[1]].Blood>0)
            {
                killelephant(farmertable[farmerno[9]],1);
                farmerstate[9]=1;
            }
            else
            {
                farmerstate[9]=0;
                if(farmertable[farmerno[9]].NowState==HUMAN_STATE_IDLE)
                    hunting(farmertable[farmerno[9]],RESOURCE_ELEPHANT);
            }
        }
        else
        {
            killelephant(farmertable[farmerno[9]],0);
            farmerstate[9]=1;
        }
        //猎杀大象

        if(farmerstate[9]==0&&farmertable[farmerno[9]].NowState==HUMAN_STATE_IDLE&&sn_to_obj(farmertable[farmerno[9]].WorkObjectSN)!=RESOURCE_ELEPHANT)
            cuttree(farmertable[farmerno[9]]);
        //大象采完去砍树
    }

    if(armycamp&&(info.armies.size()<5||!out))
        makearmy(); //兵力不足时造兵
    if(((info.armies.size()>=30||armynum>=25)&&usetool&&archerydefense&&infantrydefense&&cavalrymandefense)||(armynum+enemy>=30&&armynum>=5)||out)
    {
        out=1;
        findgold();
        //兵力充足或地方大陆威胁消除时出发找金矿
        if(farmernum<17)
        {
            for(tagBuilding&building:info.buildings)
            {
                if(building.Type==BUILDING_CENTER&&building.Project==0)
                {
                    if(food>=BUILDING_CENTER_CREATEFARMER_FOOD&&info.farmers.size()+info.armies.size()<info.Human_MaxNum)
                    {
                        BuildingAction(building.SN,BUILDING_CENTER_CREATEFARMER);
                        food-=BUILDING_CENTER_CREATEFARMER_FOOD;
                        //村民数少于17且食物充足人口未达上限则生产村民
                    }
                }
            }
        }
        if(farmernum>=17)
        {
            for(int i=2;i<farmernum;i++)
                if((sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=RESOURCE_TREE&&sn_to_obj(farmertable[farmerno[i]].WorkObjectSN)!=RESOURCE_TREE)||farmertable[farmerno[i]].NowState==HUMAN_STATE_IDLE)
                    cuttree(farmertable[farmerno[i]]);
            //村民数量达到17则耕田或空闲的村民去砍树
        }

    }
    if(armycamp&&range&&stock>=3&&farm>=5)
    {
        if(transportboat<3)
        {
            for(tagBuilding&building:info.buildings)
            {
                if(building.Type==BUILDING_DOCK&&building.Project==0)
                {
                    if(wood>=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD)
                    {
                        BuildingAction(building.SN,BUILDING_DOCK_CREATE_WOOD_BOAT);
                        wood-=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD;
                        //运输船数量少于3艘且木材充足则造运输船
                    }
                }
            }
        }
    }
    if(transportboat>=3)
    {
        for(int i=0;i<3;i++)
        {
            if(farmertable[boatno[i]].Resource==0&&farmertable[boatno[i]].NowState==HUMAN_STATE_IDLE&&!boatstate[i])
            {
                findcoast(landx,landy);
                HumanMove(boatno[i],BLOCKSIDELENGTH*landx+10.0,BLOCKSIDELENGTH*landy+10.0);
                boatstate[i]=1;
                //运输船靠岸
            }
        }
    }
    if(info.farmers.size()+info.armies.size()==48&&homenum<12&&wood>=BUILD_HOUSE_WOOD)
    {
        build(farmertable[farmerno[0]],BUILDING_HOME,homex,homey);
        wood-=BUILD_HOUSE_WOOD;
        homenum++;
        //人口达上限时再建一个房屋
    }
    return;
}


void UsrAI::f4()    //阶段4
{
    static int p=0;
    for(tagArmy&army:info.armies)
    {
        if(army.Sort!=AT_SHIP&&army.NowState==HUMAN_STATE_IDLE)
        {
            int x,y;
            int m=128*128+128*128;
            for(int j=1;j<127;j++)
            {
                for(int i=1;i<127;i++)
                {
                    if((*info.theMap)[i][j].type==MAPPATTERN_GRASS&&(nowmap[i+1][j]=='?'||nowmap[i][j+1]=='?'||nowmap[i-1][j]=='?'||nowmap[i][j-1]=='?'))
                    {
                        if(m>(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR)&&(i-army.BlockDR)*(i-army.BlockDR)+(j-army.BlockUR)*(j-army.BlockUR)>5)
                        {
                            x=i;
                            y=j;
                        }
                    }
                }
            }
            HumanMove(army.SN,x*BLOCKSIDELENGTH+10.0,y*BLOCKSIDELENGTH+10.0);
            //探索敌方大陆
        }
    }
    fighting(); //战斗
    if(transportboat<3)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_DOCK&&building.Project==0)
            {
                if(wood>=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD)
                {
                    BuildingAction(building.SN,BUILDING_DOCK_CREATE_WOOD_BOAT);
                    wood-=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD;
                    //运输船数量少于3艘且木材充足则造运输船
                }
            }
        }
    }
    if(transportboat>=3)
    {
        for(int i=0;i<3;i++)
        {
            if(farmertable[boatno[i]].Resource==0&&farmertable[boatno[i]].NowState==HUMAN_STATE_IDLE&&!boatstate[i])
            {
                findcoast(landx,landy);
                HumanMove(boatno[i],BLOCKSIDELENGTH*landx+10.0,BLOCKSIDELENGTH*landy+10.0);
                boatstate[i]=1;
                //运输船靠岸
            }
        }
    }
    if(farmernum==17&&!p)
    {
        for(int i=0;i<3;i++)
            if(farmertable[boatno[i]].Resource<5&&boatstate[i]==1)
                for(int j=i*5+2;j<i*5+7;j++)
                    if(sn_to_obj(farmertable[farmerno[j]].WorkObjectSN)!=1)
                        HumanAction(farmerno[j],boatno[i]);
        //村民登船
    }
    if(farmernum<17)
    {
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_CENTER&&building.Project==0&&food>=BUILDING_CENTER_CREATEFARMER_FOOD&&info.armies.size()+info.farmers.size()<=info.Human_MaxNum)
            {
                BuildingAction(building.SN,BUILDING_CENTER_CREATEFARMER);
                food-=BUILDING_CENTER_CREATEFARMER_FOOD;
                //村民数少于17且食物充足人口未达上限则生产村民
            }
        }
    }

    if(!p&&farmertable[boatno[0]].Resource==5&&farmertable[boatno[1]].Resource==5&&farmertable[boatno[2]].Resource==5)
        p++;    //记录是否完成登船
    if(farmernum>=17)
    {
        //升级各种可升级的技术
        for(tagBuilding&building:info.buildings)
        {
            if(building.Type==BUILDING_MARKET&&building.Project==0)
            {
                if(food>=BUILDING_MARKET_GOLD_UPGRADE_FOOD&&wood>=BUILDING_MARKET_GOLD_UPGRADE_WOOD&&!goldskill)
                {
                    BuildingAction(building.SN,BUILDING_MARKET_GOLD_UPGRADE);
                    food-=BUILDING_MARKET_GOLD_UPGRADE_FOOD;
                    wood-=BUILDING_MARKET_GOLD_UPGRADE_WOOD;
                    goldskill++;
                    continue;
                }
                if(food>=BUILDING_MARKET_STONE_UPGRADE_FOOD&&!stoneskill)
                {
                    BuildingAction(building.SN,BUILDING_MARKET_STONE_UPGRADE);
                    food-=BUILDING_MARKET_STONE_UPGRADE_FOOD;
                    stoneskill++;
                    continue;
                }
                if(food>=BUILDING_MARKET_FARM_UPGRADE_FOOD&&!farmskill)
                {
                    BuildingAction(building.SN,BUILDING_MARKET_FARM_UPGRADE);
                    food-=BUILDING_MARKET_FARM_UPGRADE_FOOD;
                    farmskill++;
                    continue;
                }
            }
            if(building.Type==BUILDING_STOCK&&building.Project==0&&(goldskill||(food>250&&wood>=100)))
            {
                if(food>=BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY_FOOD&&!infantrydefense)
                {
                    BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY);
                    food-=BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY_FOOD;
                    infantrydefense++;
                    continue;
                }
                if(food>=BUILDING_STOCK_UPGRADE_USETOOL_FOOD&&!usetool)
                {
                    BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_USETOOL);
                    food-=BUILDING_STOCK_UPGRADE_USETOOL_FOOD;
                    usetool++;
                    continue;
                }
                if(food>=BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER_FOOD&&!archerydefense)
                {
                    BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER);
                    food-=BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER_FOOD;
                    archerydefense++;
                    continue;
                }
                if(food>=BUILDING_STOCK_UPGRADE_DEFENSE_RIDER_FOOD&&!cavalrymandefense)
                {
                    BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_RIDER);
                    food-=BUILDING_STOCK_UPGRADE_DEFENSE_RIDER_FOOD;
                    cavalrymandefense++;
                    continue;
                }
           }
        }
    }
    if(p)
    {
        landing();  //登陆
        getgold();  //获取黄金
    }
    return;
}


void UsrAI::processData()
{
    info=getInfo(); //获取当前帧数据
    newland=0;
    gold=0;
    armynum=0;
    getmap();   //获取地图
    gethuman(); //获取人员信息
    getresource();  //获取资源信息

    switch (state)
    {
    case 0:f0();
           break;   //游戏开始
    case 1:f1();
           break;   //阶段1
    case 2:f2();
           break;   //阶段2
    case 3:f3();
           break;   //阶段3
    case 4:f4();
           break;   //阶段4
    }
    sheep.clear();
    sheepst.clear();
    //清空

    if(info.GameFrame>=(27*60+30)*25&&state==3)
        state=4;
    //阶段3失败强行登陆
}


